package com.example.splash

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.base.BaseActivity
import com.example.path.RouterPath
import com.example.splash.databinding.ActivitySplashBinding
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    var imageData = listOf(R.drawable.bg1,R.drawable.bg2,R.drawable.bg3)

    override fun getLayoutId(): Int =R.layout.activity_splash

    override fun initViews() {

        //判断是否第一次打开APP
        var isFirst =MMKV.defaultMMKV().decodeBool("isFirst",true)
        if (isFirst){
            MMKV.defaultMMKV().encode("isFirst",false)
            mDataBinding.clGuile.visibility=View.VISIBLE
            mDataBinding.tvSkip.visibility = View.GONE
            mDataBinding.ivBanner.setAdapter(GuideAdapter(imageData))
            mDataBinding.ivBanner.isAutoLoop(false)
            mDataBinding.ivBanner.indicator = CircleIndicator(this)
            mDataBinding.ivBanner.start()
            //Banner滑动事件
            mDataBinding.ivBanner.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                }
                override fun onPageSelected(position: Int) {
                    //最后显示TextView
                    if(position == imageData.size - 1){
                        mDataBinding.tvSkip.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvSkip.visibility = View.INVISIBLE
                    }
                }
                override fun onPageScrollStateChanged(state: Int) {

                }
            })

            //点击事件
            mDataBinding.tvSkip.setOnClickListener {
                startToMain()
            }
        }else{
           startToMain()
        }
    }

    private fun startToMain() {
        var token =MMKV.defaultMMKV().decodeString("token","")
        var path = if(!token.isNullOrEmpty()) RouterPath.PATH_MAIN else RouterPath.PATH_LOGIN
        ARouter.getInstance().build(path).navigation()
        this@SplashActivity.finish()
    }

    override fun onStart() {
        super.onStart()
        mDataBinding.ivBanner.start()
    }

    override fun onStop() {
        super.onStop()
        mDataBinding.ivBanner.stop()
    }

    override fun onDestroy() {
        super.onDestroy()
        mDataBinding.ivBanner.destroy()
    }



}